<?php
/**
 * Analysis of data from the file .Ini (app/etc/config.ini)
 *
 * @category   Epic
 * @package    Epic_Config
 * @author     Epic Core Team <nguyendnhat@gmail.com>
 */
class Epic_Config_Ini
{
	/*
	* The path file config
	* @var string
	*/
	private $_configFile = 'etc/config.ini';

	/*
	 * Store values of file config 
	 * @var string
	 */
	private $_configValues = array();

	/**
     * Singleton instance
     * @var Epic_Config_Ini
     */
	private static $instance = null;

	/**
     * Singleton instance
     *
     * @return Epic_Config_Ini
     */
	public static function getInstance()
	{
 		if(!(self::$instance)) {
 			self::$instance = new self();
 		}
		return self::$instance;
	}

    /**
     * Enforce singleton; disallow cloning
     *
     * @return void
     */
	private function __clone() {}

	/**
     * Constructor
     *
     * Instantiate using {@link getInstance()}; Epic_Config_Ini is a singleton
     * object.
     *
     * Analysis of data from the file. Ini put into an array
     *
     * @return void
     */
	private function __construct()
	{
		$this->_configValues = parse_ini_file(APP_DIR . $this->_configFile, true);
		if (self::getConfig('app/timezone')) {
			date_default_timezone_set(self::getConfig('app/timezone'));
		}
		$this->_setReporting();
	}

	/*
	 * Check if environment is development and display errors
	 */
	private function _setReporting() 
	{
		if (self::getConfig('debug/development_evironment') == true) {
			error_reporting(E_ALL);
			ini_set('display_errors','On');
		} else {
			error_reporting(E_ALL);
			ini_set('display_errors','Off');
			ini_set('log_errors', 'On');
			ini_set('error_log', ERROR_DIR . 'log' . DS . 'error.log');
		}
	}

	/**
	 * Retrieve the default module name in file config
	 * 
	 * @return string
	 */
	public function getDefaultModule()
	{
		return $this->_configValues['app']['default_module'];
	}
	
	/**
	 * Retrieve the default controller name in file config
	 * 
	 * @return string
	 */
	public function getDefaultController()
	{
		return $this->_configValues['app']['default_controller'];
	}
	
	/**
	 * Retrieve the default action name in file config
	 * 
	 * @return string
	 */
	public function getDefaultAction()
	{
		return $this->_configValues['app']['default_action'];
	}
	
	/**
	 * Retrieve the value in file config
	 * 
	 * @param string $value
	 * @return string
	 */
	public function getConfig($value)
	{
		$config = explode("/", $value);
		return $this->_configValues[$config[0]][$config[1]];
	}
}
